package com.example.testapp.activity.activity.menu;

import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

import com.example.testapp.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Created by l on 2016/6/30.
 */
public class DateTimeActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_picker);

        initView();
    }

    private void initView() {
        DatePicker datePicker = (DatePicker) findViewById(R.id.dpPicker);
        TimePicker timePicker = (TimePicker)findViewById(R.id.tpPicker);

        datePicker.init(Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar calendar = Calendar.getInstance();
                calendar.set(year, monthOfYear, dayOfMonth);
                SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日  HH:mm");
                Toast.makeText(DateTimeActivity.this, format.format(calendar.getTime()), Toast.LENGTH_SHORT).show();
            }
        });

        timePicker.setIs24HourView(true);
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                Toast.makeText(DateTimeActivity.this, hourOfDay + "小时" + minute + "分钟", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
